home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / c / flash-0.4.3.lha / flash-0.4.3 / Lib / displaylist.cc < prev    next >
C/C++ Source or Header  |  1999-02-21  |  5KB  |  240 lines

  1. ////////////////////////////////////////////////////////////
  2. // Flash Plugin and Player
  3. // Copyright (C) 1998,1999 Olivier Debon
  4. // 
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. // 
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. // 
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. // 
  19. ///////////////////////////////////////////////////////////////
  20. //  Author : Olivier Debon  <odebon@club-internet.fr>
  21. //  
  22.  
  23. #include <stdio.h>
  24. #include <assert.h>
  25.  
  26. static char *rcsid = "$Id: displaylist.cc,v 1.7 1999/02/02 18:45:05 olivier Exp $";
  27.  
  28. #include "displaylist.h"
  29. #include "graphic.h"
  30. #include "character.h"
  31.  
  32. #define PRINT 0
  33.  
  34. DisplayList::DisplayList()
  35. {
  36.     list = 0;
  37.     bg = 0;
  38. }
  39.  
  40. DisplayList::~DisplayList()
  41. {
  42.     clearList();
  43. }
  44.  
  45. void
  46. DisplayList::clearList()
  47. {
  48.     DisplayListEntry *del, *e;
  49.  
  50.     for(e = list; e;)
  51.     {
  52.         del = e;
  53.         e = e->next;
  54.         delete del;
  55.     }
  56.     list = 0;
  57. }
  58.  
  59. DisplayListEntry *
  60. DisplayList::getList()
  61. {
  62.     return list;
  63. }
  64.  
  65. void
  66. DisplayList::placeObject(Character *character, long depth, Matrix *matrix, Cxform *cxform)
  67. {
  68.     DisplayListEntry *n,*e,*prev;
  69.  
  70.     n = new DisplayListEntry;
  71.     n->depth = depth;
  72.     n->matrix = matrix;
  73.     n->cxform = cxform;
  74.     n->character = character;
  75.  
  76.     if (character == 0 || matrix == 0 || cxform == 0) {
  77.         for (e = list; e; prev = e, e = e->next) {
  78.             if (e->depth == n->depth) {
  79.                 if (character == 0) {
  80.                     n->character = e->character;
  81.                 }
  82.                 if (matrix == 0) {
  83.                     n->matrix = e->matrix;
  84.                 }
  85.                 if (cxform == 0) {
  86.                     n->cxform = e->cxform;
  87.                 }
  88.                 break;
  89.             }
  90.         }
  91.     }
  92.  
  93.     if (n->character == 0) {
  94.         // Not found !!!    Should not happen
  95.         printf("PlaceObject cannot find character at depth %d\n", n->depth);
  96.         delete n;
  97.         return;
  98.     }
  99.  
  100.     prev = 0;
  101.     for (e = list; e; prev = e, e = e->next)
  102.     {
  103.         if (e->depth == n->depth) {
  104.             // Replace object
  105.             if (prev) {
  106.                 prev->next = e->next;
  107.                 delete e;
  108.                 e = prev->next;
  109.             } else {
  110.                 list = e->next;
  111.                 delete e;
  112.                 e = list;
  113.             }
  114.             if (e) {
  115.                 // Should break then
  116.                 assert(e->depth > n->depth);
  117.             } else {
  118.                 break;
  119.             }
  120.         }
  121.         if (e->depth > n->depth) break;
  122.     }
  123.     if (prev == 0) {
  124.         // Object comes at first place
  125.         n->next = list;
  126.         list = n;
  127.     } else {
  128.         // Insert object
  129.         n->next = prev->next;
  130.         prev->next = n;
  131.     }
  132. }
  133.  
  134. Character *
  135. DisplayList::removeObject(Character *character, long depth)
  136. {
  137.     DisplayListEntry *e,*prev;
  138.  
  139.     // List should not be empty
  140.     if (list == 0) return 0;
  141.  
  142.     prev = 0;
  143.     for (e = list; e; prev = e, e = e->next)
  144.     {
  145.         if (e->depth == depth) {
  146.              if (prev) {
  147.                 prev->next = e->next;
  148.             } else {
  149.                 list = e->next;
  150.             }
  151.             if (character == 0) {
  152.                 character = e->character;
  153.             }
  154.             delete e;
  155.             return character;
  156.         }
  157.     }
  158.     return 0;    // Should not happen
  159. }
  160.  
  161. Character *
  162. DisplayList::removeObject(long depth)
  163. {
  164.     return removeObject((Character *)0,depth);
  165. }
  166.  
  167. ActionRecord *
  168. DisplayList::processEvent(GraphicDevice *gd, FlashEvent *event)
  169. {
  170.     DisplayListEntry *e;
  171.     ActionRecord     *action = 0;
  172.  
  173.     for (e = list; e; e = e->next)
  174.     {
  175.         if (e->character) {
  176.             if (e->character->hasEventHandler()){
  177.                 action = e->character->eventHandler(gd, event);
  178.                 if (action) {
  179.                     break;
  180.                 }
  181.             }
  182.         }
  183.     }
  184.  
  185.     return action;
  186. }
  187.  
  188. void
  189. DisplayList::setBackgroundColor(Color *color)
  190. {
  191.     bg = color;
  192. }
  193.  
  194. int
  195. DisplayList::render(GraphicDevice *gd, Matrix *m)
  196. {
  197.     DisplayListEntry *e;
  198.     int sprite = 0;
  199.     long n = 0;
  200.  
  201.     if (bg) {
  202.         gd->setBackgroundColor(*bg);
  203.         bg = 0;
  204.     }
  205.  
  206.     for (e = list; e; e = e->next)
  207.     {
  208. #if PRINT
  209.         printf("Character %3d @ %3d\n", e->character ? e->character->getTagId() : 0, e->depth);
  210. #endif
  211.         if (e->character) {
  212.             Matrix mat;
  213.  
  214.             if (m) {
  215.                 mat = *m;
  216.             }
  217.  
  218.             if (e->matrix) {
  219.                 mat = mat * (*e->matrix);
  220.             }
  221.  
  222.             if (e->character->hasEventHandler()) {
  223.                 long hitTestId;
  224.  
  225.                 hitTestId = gd->registerHitTest(e->character->getTagId());
  226.                 if (hitTestId) {
  227.                     e->character->getRegion(gd, &mat, hitTestId);
  228.                 }
  229.             }
  230.  
  231.             if (e->character->execute(gd, &mat, e->cxform)) {
  232.                 sprite = 1;
  233.             }
  234.  
  235.             n++;
  236.         }
  237.     }
  238.     return sprite;
  239. }
  240.